﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.ViewInfo;
using System.Drawing;
using DTD2013.Common;
using DevExpress.Utils.Drawing;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Columns;

namespace DTD2013.DTDComponent
{
    public partial class DTDTreeListControl : TreeList
    {
        public DTDTreeListControl()
        {
            InitializeComponent();
            InitializeControl();
        }

        public DTDTreeListControl(IContainer container) : this()
        {
            container.Add(this);
        }

        protected override TreeListViewInfo CreateViewInfo()
        {
            return new DTDTreeListViewInfo(this);
        }

        private void InitializeControl()
        {
            this.OptionsBehavior.AutoPopulateColumns = false;
            this.OptionsBehavior.AutoMoveRowFocus = true;
            this.OptionsBehavior.EnterMovesNextColumn = true;
            this.OptionsBehavior.AutoSelectAllInEditor = true;
            this.OptionsBehavior.ShowEditorOnMouseUp = true;
            
            this.OptionsView.AutoWidth = false;

            this.Appearance.HeaderPanel.Font = new Font(AppearanceRow.HeaderFontName, AppearanceRow.HeaderFontHeight);
            this.Appearance.HeaderPanel.Options.UseFont = true;
            this.Appearance.Row.Font = new Font(AppearanceRow.RowFontName, AppearanceRow.RowFontHeight);
            this.Appearance.Row.Options.UseFont = true;
            this.ColumnPanelRowHeight = AppearanceRow.ColumnHeight;
            this.RowHeight = AppearanceRow.RowHeight;
            // Disable sort
            foreach(TreeListColumn column in this.Columns)
            {
                column.OptionsColumn.AllowSort = false;
            }

            // Event handler
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(DTDTreeListControl_MouseClick);
        }

        #region Virtual method

        protected virtual void DTDTreeListControl_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu menu = new ContextMenu();
                menu.MenuItems.Add(Constant.OPEN_ALL, new EventHandler(ExpandAll_Clicked));
                menu.MenuItems.Add(Constant.COLLAPSE_ALL, new EventHandler(CollapseAll_Clicked));
                menu.Show(this, new Point(e.X, e.Y));
            }
        }

        protected virtual void DTDTreeListControl_CustomDrawNodeIndicator(object sender, CustomDrawNodeIndicatorEventArgs e)
        {
            TreeList tree = sender as TreeList;
            IndicatorObjectInfoArgs arg = e.ObjectArgs as IndicatorObjectInfoArgs;
            arg.DisplayText = (tree.GetVisibleIndexByNode(e.Node) + 1).ToString();
            e.Appearance.Font = new Font(e.Appearance.Font.FontFamily, AppearanceRow.RowIndicatorText);
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;

            //Graphics gr = e.Graphics;
            //gr.PageUnit = GraphicsUnit.Pixel;
            //SizeF size = gr.MeasureString(tree.GetVisibleIndexByNode(e.Node).ToString(), e.Appearance.Font);
            //tree.IndicatorWidth = Convert.ToInt32(size.Width) + 5;
        }

        #endregion

        #region Private method

        private void ExpandAll_Clicked(object sender, EventArgs e)
        {
            this.ExpandAll();
        }

        private void CollapseAll_Clicked(object sender, EventArgs e)
        {
            this.CollapseAll();
        }

        #endregion
    }
}
